package com.health.system.config;

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;

/**
 * 跨域配置
 *
 */
public class CorsConfig implements CorsConfigurationSource {
    @Override
    public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
        CorsConfiguration config = new CorsConfiguration();
        // 允许的请求头字段
        config.setAllowedHeaders(Collections.singletonList("*"));
        // 允许的请求方法
        config.setAllowedMethods(Collections.singletonList("*"));
        // 允许的域
        config.setAllowedOrigins(Collections.singletonList("*"));
        // 预检请求有效期
        config.setMaxAge(3600L);
        // 拦截所有请求
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", config);
        return config;
    }
}
